﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace working
{
    public partial class SettingLinkForm : Form
    {
        public Action onChange { get; set; }
        public ConfigLogic configLogic { get { return ConfigLogic.Instence; } }
        public ProjectArray projectArray { get { return configLogic.projectArray; } }
        public SettingLinkForm()
        {
            InitializeComponent();
            UpdateListView();
            listView1.MouseClick += OnListItemClick;
        }
        public void ShowSetting()
        {
            Show();
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
            e.Cancel = true;
            configLogic.SaveConfigProject();
            Hide();
        }

        private void UpdateListView()
        {
            listView1.Items.Clear();

            if(projectArray != null)
            {
                for (int i = 0; i < projectArray.projectNames.Count; i++)
                {
                    ListViewItem item = new ListViewItem();
                    item.Text = projectArray.projectNames[i];
                    item.SubItems.Add(projectArray.projectPaths[i]);
                    listView1.Items.Add(item);
                }
            }
            listView1.Update();

            if (onChange != null){
                onChange.Invoke();
            }
        }

        private void OnListItemClick(object sender, MouseEventArgs e)
        {
            var item0 = listView1.SelectedItems[0];
            var id = projectArray.projectNames.FindIndex(x => x == item0.Text);
            if (id >= 0)
            {
                textBox_name.Text = projectArray.projectNames[id];
                textBox_path.Text = projectArray.projectPaths[id];
            }
            if(e.Button == MouseButtons.Right)
            {
                toolStripMenuItem_moveup.Enabled = GetSelectedListViewIndex() > 0;
                toolStripMenuItem_movedown.Enabled = GetSelectedListViewIndex() < projectArray.projectNames.Count - 1;
                contextMenuStrip1.Show(MousePosition);
            }
        }

        private void addProj_Click(object sender, EventArgs e)
        {
            var index = projectArray.projectNames.IndexOf(textBox_name.Text);
            if(index >= 0)
            {
                projectArray.projectPaths[index] = textBox_path.Text;
            }
            else
            {
                projectArray.projectNames.Add(textBox_name.Text);
                projectArray.projectPaths.Add(textBox_path.Text);
            }
            UpdateListView();
        }
        private string GetProjectPath(string name)
        {
            if(Util.ExistConfigItem(name)){
                return Util.GetConfigItem(name);
            }
            return null;
        }
        private int GetSelectedListViewIndex()
        {
            if (listView1.SelectedItems.Count > 0)
            {
                var text = listView1.SelectedItems[0];
                var index = projectArray.projectNames.IndexOf(text.Text);
                return index;
            }
            else
            {
                return -1;
            }
        }
        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(MessageBox.Show("确认删除!","如果删除将会删除导航下所有配制！",MessageBoxButtons.OKCancel) != DialogResult.OK) {
                return;
            }
            var index = GetSelectedListViewIndex();
            if (index >= 0)
            {
                projectArray.projectNames.RemoveAt(index);
                projectArray.projectPaths.RemoveAt(index);
                UpdateListView();
            }
        }

        private void toolStripMenuItem_moveup_Click(object sender, EventArgs e)
        {
            var index = GetSelectedListViewIndex();
            if (index > 0)
            {
                var tempName = projectArray.projectNames[index];
                var tempPath = projectArray.projectPaths[index];
                projectArray.projectNames[index] = projectArray.projectNames[index - 1];
                projectArray.projectPaths[index] = projectArray.projectPaths[index - 1];
                projectArray.projectNames[index - 1] = tempName;
                projectArray.projectPaths[index - 1] = tempPath;
                UpdateListView();
            }
        }

        private void toolStripMenuItem_movedown_Click(object sender, EventArgs e)
        {
            var index = GetSelectedListViewIndex();
            if (index >= 0 && index < projectArray.projectNames.Count - 1)
            {
                var tempName = projectArray.projectNames[index];
                var tempPath = projectArray.projectPaths[index];
                projectArray.projectNames[index] = projectArray.projectNames[index + 1];
                projectArray.projectPaths[index] = projectArray.projectPaths[index + 1];
                projectArray.projectNames[index + 1] = tempName;
                projectArray.projectPaths[index + 1] = tempPath;
                UpdateListView();
            }
        }
    }
}
